<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Model\Article;

class EssenceController extends Controller
{
    public function iteration()
    {
        return view('essence.iteration')->with([
            'articles' => Article::all(),
        ]);
    }

    public function event()
    {
        return view('essence.event')->with([

        ]);
    }

    public function __call($method, $parameters)
    {
        return view(resolveView(__CLASS__, $method));
    }


    public function getSortImages()
    {
        $path = Storage::disk('assets');
        $files = $path->allFiles('upload/placeholder');

        usort($files, function($a, $b) {
            $pattern = '/[^\d]*([0-9]*)[^\d]*/';
            $ida = preg_replace($pattern, '$1', $a);
            $idb = preg_replace($pattern, '$1', $b);

            return $ida >= $idb ? 1 : -1;
        });

        $data = [];
        foreach ($files as $key => $val) {
            $data[] = [
                'key' => $key,
                'val' => '/' . $val,
            ];
        }

        return $data;
    }
}
